Conditions | 26 |
Total Lines | 52 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like jquery.midcom_services_toolbars.js ➔ attach_events often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | $.fn.extend({ |
||
69 | function attach_events() { |
||
70 | var item_holder = $('div.items', root_element); |
||
71 | |||
72 | $('div.item', item_holder).each(function(i, n) { |
||
73 | var item = $(n), |
||
74 | handle = $('.midcom_services_toolbars_topic_title', item), |
||
75 | children = $('ul', item); |
||
76 | |||
77 | item.on('mouseover', function() { |
||
78 | clearTimeout(item_holder.data("hide")); |
||
79 | $('.item ul', item_holder).hide(); |
||
80 | $('.midcom_services_toolbars_topic_title.hover', item_holder).removeClass("hover"); |
||
81 | handle.addClass("hover"); |
||
82 | children.show(); |
||
83 | }); |
||
84 | item.on('mouseout',function() { |
||
85 | item_holder.data('hide', setTimeout(function() { |
||
86 | handle.removeClass("hover"); |
||
87 | children.hide(); |
||
88 | }, 1000)); |
||
89 | }); |
||
90 | }); |
||
91 | |||
92 | minimizer.on('click', toggle_visibility); |
||
93 | root_element.find('.minimizer').on('click', toggle_visibility); |
||
94 | |||
95 | root_element.draggable({ |
||
96 | start: function() { |
||
97 | if (root_element.hasClass('type_menu')) { |
||
98 | root_element |
||
99 | .removeClass('type_menu') |
||
100 | .addClass('type_palette switch_to_palette'); |
||
101 | } |
||
102 | }, |
||
103 | drag: function(event, ui) { |
||
104 | if (root_element.hasClass('switch_to_palette')) { |
||
105 | root_element.removeClass('switch_to_palette'); |
||
106 | //TODO: this should work, but doesn't... |
||
107 | ui.position.left = $(window).width() - root_element.width(); |
||
108 | } else if (ui.position.top === 0) { |
||
109 | root_element |
||
110 | .removeClass('type_palette') |
||
111 | .addClass('type_menu'); |
||
112 | } |
||
113 | }, |
||
114 | stop: function() { |
||
115 | save_settings(true); |
||
116 | }, |
||
117 | containment: 'window', |
||
118 | cancel: '.items ul' |
||
119 | }); |
||
120 | } |
||
121 | |||
173 |